{extend name="public/container"}
{block name='head_top'}
<script type="text/javascript" charset="utf-8" src="{__ADMIN_PATH}plug/ueditor/third-party/zeroclipboard/ZeroClipboard.js"></script>
<script type="text/javascript" charset="utf-8" src="{__ADMIN_PATH}plug/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="{__ADMIN_PATH}plug/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript" src="{__ADMIN_PATH}plug/ueditor/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="{__PC_KS3}src/async.min.js"></script>
{/block}
{block name="content"}
<style type="text/css">
    /** 分销弹窗样式重置开始 */
    .avatar-uploader .el-upload {
        width: 200px;
        height: 300px;
    }
    .el-dialog__body {padding: 10px 20px;}
    .el-form-item {margin-bottom: 18px;}
    /** 分销弹窗样式重置开始 */
</style>
<div id="myApp" v-cloak>
    <div class="padding-10">
        <div class="douxue-filter flex flex-between" style="margin-bottom:20px;">
            <el-form :inline="true" :model="where" size="mini" class="flex">
                <div class="flex flex-between">
                    <el-form-item>
                        <el-input size="small" v-model="where.name" placeholder="{:lang('Goods')}名称">
                            <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                        </el-input>
                    </el-form-item>
                </div>
            </el-form>

            <el-button type="primary" size="mini" @click="doAdd()">新增{:lang('Goods')}</el-button>
        </div>
        <div class="">
            <el-table
                :data="rows"
                style="width: 100%"
                v-loading="loading"
                element-loading-text="加载中"
                element-loading-spinner="el-icon-loading"
                size="mini">
                <el-table-column
                    prop="id"
                    label="ID"
                    width="60"
                    align="left">
                </el-table-column>
                <el-table-column
                    prop="name"
                    label="{:lang('Goods')}名称"
                    align="left">
                </el-table-column>
                <el-table-column
                    prop="goods_type_text"
                    width="140"
                    label="{:lang('Goods')}类型"
                    align="left">
                </el-table-column>
                <!-- <el-table-column
                    prop="cover"
                    label="封面图"
                    width="140"
                    align="center">
                    <template slot-scope="scope">
                        <el-image
                            v-if="scope.row.cover.pic != undefined "
                            style="height: 60px"
                            fit="contain"
                            :src="scope.row.cover.pic "
                            :preview-src-list="[scope.row.cover.pic]">
                        </el-image>
                        <div v-else class="image-slot">
                            <i class="el-icon-picture-outline"></i>
                        </div>
                    </template>
                </el-table-column> -->
                <el-table-column
                    label="是否启用"
                    width="160"
                    align="left">
                    <template slot-scope="scope">
                        <el-switch
                            v-model="scope.row.status"
                            active-color="#ff6a6c"
                            inactive-color="#999"
                            :active-value="1"
                            :inactive-value="0"
                            @change="doSetStatus(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    fixed="right"
                    width="140"
                    align="left">
                    <template slot-scope="scope">
                        <el-link type="success" size="mini" :underline="false" @click="doLoadSkuList(scope.row.id)">SKU</el-link>
                        <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row.id)">编辑</el-link>
                        <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    small
                    :hide-on-single-page="true"
                    layout="prev, pager, next"
                    :total="rowsCount"
                    :page-size="where.limit"
                    @current-change="doLoadRows">
                </el-pagination>
            </div>
        </div>
    </div>

     <!-- 价格设置   -->
    <el-dialog title="SKU设置" :visible.sync="priceCfgFlag" @close="closeSkuPage">
        <el-table
            :data="skuList"
            style="width: 100%"
            v-loading="loading"
            element-loading-text="加载中"
            element-loading-spinner="el-icon-loading"
            size="mini">
            <el-table-column
                prop="id"
                label="ID"
                width="60"
                align="left">
            </el-table-column>
            <el-table-column
                prop="sku_name"
                label="SKU名称"
                align="left">
            </el-table-column>


            <el-table-column
              align="right">
              <template slot="header" slot-scope="scope">
                <el-input v-model="skuForm.sku_name" maxlength="20" size="mini" show-word-limit placeholder="SKU名称" v-if="isNewAddSku==true"></el-input>
                <el-link :underline="false" type="primary" v-if="isNewAddSku==true" @click="saveSku()">保存</el-link>
                <el-link :underline="false" v-if="isNewAddSku==true" @click="isNewAddSku = false">取消</el-link>
                <el-button
                  size="mini"
                  v-if="isNewAddSku==false"
                  @click="isNewAddSku = true"
                  >新增SKU</el-button>

              </template>
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  @click=""
                  icon="el-icon-setting"
                  ></el-button>
                <el-button
                  size="mini"
                  icon="el-icon-delete"
                  type="danger"
                  ></el-button>
              </template>
            </el-table-column>
        </el-table>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var my = new Vue({
        el: "#myApp",
        data(){
            return {
                where:{
                    name:'',
                    page:1,
                    limit:pageSize
                },
                loading:false,
                rowsCount: 0,
                rows: [],
                GoodsTypes:[],
                priceCfgFlag: false,
                priceLoaded: false,
                priceCfg: {},
                isNewAddSku: false,
                skuForm: {
                    sku_name: '',
                    goods_id: 0
                },
                skuList: []
            }
        },
        methods: {
            cateFilter (cate_id) {
                this.where.cate_id = cate_id
                this.doLoadRows()
            },
            doLoadRows (page) {
                this.loading = true
                this.where.page  = page || 1
                axios.get(Url({a:'get_list' , q:this.where}))
                    .then(res => {
                        this.rows = res.data.data
                        this.rowsCount =res.data.count
                        this.loading = false
                    })
            }
            ,closeSkuPage(){
                this.isNewAddSku = false
                this.skuForm.sku_name = ''
            }
            ,doSetStatus (row) {
                axios.get(Url({a:'set_status',q:{status:row.status,id:row.id}}))
                    .then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                            onClose: _ => {
                                if (res.data.code !== 200) {
                                    this.doLoadRows()
                                }
                            }
                        })
                    })
            },
            // 价格设置
            doLoadSkuList (goods_id) {
                this.priceCfgFlag = true
                this.skuForm.goods_id = goods_id
                axios.get(Url({a:'sku_list',q:{goods_id:goods_id}}))
                    .then(res => {
                        //this.priceLoaded = true
                        this.skuList = res.data.data
                    })
            }
            ,saveSku () {
                if ( !this.skuForm.goods_id ) return
                axios.post( Url({a:'sku_save'}) , this.skuForm)
                    .then(res => {
                        this.$message({
                            message:'操作成功',
                            type:'success',
                            duration:1500
                        });
                        this.isNewAddSku = false;
                        this.doLoadSkuList(this.skuForm.goods_id)
                    }).catch(_ => {
                    this.$message({
                        message: '操作失败',
                        type: 'error'
                    })
                })
            }
            ,doAdd (id) {
                window.location.href = Url({a:'add',q:{id:id}})
            },
            doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            },
            doLoadGoodsTypes () {
                axios.get(Url({c:'goods.goods',a:'get_attr' , q:{name:'goods_type'}}))
                    .then(res => {
                        this.GoodsTypes = res.data.data
                    })
            },

            doDeleteCategory (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({c:'category.category',a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadGoodsTypes(1);
                }).catch(_ => {})
            },
            onSubmitCategory (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({c:'category.category',a:'save'}) , this.category)
                            .then(res => {
                                this.$message({
                                    message:'操作成功',
                                    type:'success',
                                    duration:1500
                                });
                                this.categoryDialogFormVisible = false;
                                this.doLoadGoodsTypes(1)
                            }).catch(_ => {
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            })
                        })
                    } else {
                        return false;
                    }
                });
            },
            handleAvatarSuccess(res, file) {
                if ( res.code == 0 ) {
                    return this.separateConfig.share_poster = res.pic || '';
                }
                return this.$message.error(res.state);
            },
            beforeAvatarUpload(file) {
                const isLt2M = file.size / 1024 / 1024 < 1;
                const isJPG = (file.type === 'image/jpeg' || file.type === 'image/png' );
                if (!isLt2M) {
                    this.$message.error('上传图片大小不能超过 1MB!');
                }

                if (!isJPG) {
                  this.$message.error('上传图片只能是 JPG或PNG 格式!');
                }

                return isJPG && isLt2M;
            }
        },
        mounted(){
            this.doLoadRows()
            this.doLoadGoodsTypes()



        }
    })
</script>
{/block}